package com.own.business.system.user.wechat.core.service.impl;

import com.own.business.system.account.business.service.SystemAccountService;
import com.own.business.system.user.business.service.SystemRegisterService;
import com.own.business.system.user.business.service.SystemUserService;
import com.own.business.system.user.common.exception.SystemUserException;
import com.own.business.system.user.wechat.core.service.SystemUserWechatLoginService;
import com.own.business.system.user.wechat.core.type.AccountType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * SystemUserWechatLoginServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-25 22:59:00
 */
@Slf4j
@Service
public class SystemUserWechatLoginServiceImpl implements SystemUserWechatLoginService {

    @Resource
    private SystemAccountService systemAccountService;
    @Resource
    private SystemRegisterService systemRegisterService;
    @Resource
    private SystemUserService systemUserService;

    /**
     * 根据openId登录
     *
     * @param openId      微信openId
     * @param accountType 账号类型 1=普通用户 2=管理员
     * @return 用户id
     */
    @Override
    public Long login(String openId, int accountType) {
        // 去账号表查询账号的信息
        var po = systemAccountService.get(openId, AccountType.WECHAT, accountType);
        if (po != null) {
            if (po.getEnable() == 0) {
                throw new SystemUserException("账号已被禁用");
            }
            return po.getFkUserId();
        }
        // 注册账号信息
        var vo = systemRegisterService.register(openId, null, AccountType.WECHAT.type(), accountType);
        return vo.getId();
    }

    /**
     * 验证用户id是否已经绑定微信号
     *
     * @param userId 用户id
     * @return 是否绑定
     */
    @Override
    public boolean isBind(Long userId) {
        return systemAccountService.get(userId, AccountType.WECHAT) != null;
    }

    /**
     * 绑定微信号
     *
     * @param userId 用户id
     * @param openId 微信openId
     */
    @Override
    public void bind(Long userId, String openId) {
        // 查询账号信息
        var user = systemUserService.getPoById(userId);
        // 查询待绑定的手机号是否已经绑定过账号了
        var po = systemAccountService.get(openId, AccountType.WECHAT, user.getType());
        if (po != null) {
            throw new SystemUserException("该微信号已经被绑定了，请先解绑");
        }
        // 判断目标的账号是否被绑定了微信号
        var other = systemAccountService.get(userId, AccountType.WECHAT);
        if (other != null) {
            throw new SystemUserException("该账号已经绑定了微信号，请先解绑");
        }
        // 创建新的账号
        systemAccountService.save(openId, AccountType.WECHAT, user.getType(), userId);
    }

    /**
     * 绑定微信号
     *
     * @param userId 用户id
     */
    @Override
    public void unbind(Long userId) {
        // 判断有几个账号信息
        var count = systemAccountService.count(userId);
        if (count <= 1) {
            throw new SystemUserException("当前账号无法解绑微信号，请先设置登录密码后解绑");
        }
        systemAccountService.remove(userId, AccountType.WECHAT);
    }

}
